package com.yf.words.service.impl;

import com.yf.words.excel.ExcelReaderUtil;
import com.yf.words.excel.Sentence;
import com.yf.words.excel.Word;
import com.yf.words.service.WordService;
import com.yf.words.util.DataUtil;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WordsServiceImpl implements WordService {

    private List<Word> words;

    private List<Sentence> sentences;

    private List<String> wordNames;

    @PostConstruct
    private void init() {
        words = DataUtil.getListData(Word.class, ExcelReaderUtil.FILE_WORDS, "words").stream()
                .filter(i -> i.getWord() != null)
                .toList();

        sentences = DataUtil.getListData(Sentence.class, ExcelReaderUtil.FILE_WORDS, "例句").stream()
                .filter(i -> i.getWord() != null)
                .toList();

        //
        wordNames = words.stream().map(Word::getWord).distinct().sorted().toList();
    }

    @Override
    public List<String> getAllWords() {
        return wordNames;
    }

    @Override
    public List<Word> getDataByWord(String word) {
        return words.stream().filter(i -> i.getWord().equals(word)).toList();
    }

    @Override
    public List<Sentence> getSentencesByWord(String word) {
        return sentences.stream().filter(i -> i.getWord().equals(word)).toList();
    }
}